from PyQt5.QtWidgets import QMainWindow,QApplication,QDesktopWidget
import sys
class Winform(QMainWindow):
    def __init__(self,parent=None):
        super(Winform,self).__init__(parent)
        self.setWindowTitle('主窗口居中')
        self.resize(300,600)
        self.center()
    def center(self):
        screen=QDesktopWidget().screenGeometry()
        size=self.geometry() #算出屏幕尺寸
        self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2)
if __name__ == '__main__':
    app=QApplication(sys.argv)
    win=Winform()
    win.show()
    sys.exit(app.exec_())